Do you have a moment to talk about our lord and savior, vim?

Waldir Leoncio

Oslo Centre for Biostatistics and Epidemiology, UiO

2025-05-20

Part 1: Testifying

From dismissal to adoption: an edited timeline

%%{init: {'theme':'dark'}}%%
gantt
    dateFormat YY
    axisFormat %Y
    Lost HPC virginity: milestone, 16, 1y
    "F this old thing, I'll just use nano" : 15, 9y
    Hired as RSE: milestone, 20, 1d
    "I will fear no more": milestone, 24, 1d
    (studies vim furiously): 24, 1y
    "I have seen the light": milestone, 25, 1d

Why should you care about this arcane program?

I mean, look at this outdated logo:

  1. But some people swear by it.
  2. And we’re computer-savvy people who like shortcuts… right?
  3. We’re RSEs, so it’s sort of our job to be on top of these things.

Slow but steady

  • Cheat sheets are a great crutch
  • It is fast even though it doesn’t feel fast
  • Downside: you look less busy

Modes are annoying but useful

Function Noob editor vim
Move around 4 arrow + 7 mod keys “Every” key (normal mode)
Select text 4 arrow + 7 mod keys “Every” key (visual mode)
Write Char keys Char keys (insert mode)
  • Changing modes takes a bit of getting used to
  • Learning so many shortcuts takes some time
    • But you don’t have to learn them all
    • And you don’t have to learn at once
    • And you will memorize the ones you use most often

A new dawn

  • Vim-shortcuts are not just for vim
  • You start getting annoyed when you don’t see them literally everywhere

Part 2: A crash-course on Vim

Step zero: acquire a pure-text file

  1. Locate some text file you’re familiar with
  2. Generate a new one
    • Lorem Ipsum generator
    • Code generator (AI)

The basics

Important

Made an oopsie? Undo and redo with u and Ctrl+r

Important

Vim shortcuts are case-sensitive i is not the same as I!

Tip

Need help? Feel like going deeper? Read docs with :help

Tip

Repeat the last action (not movement!) with .. It’s more useful than it seems!

Hand position

  1. Find those bumps on the f and j keys.
  2. Put your index fingers on them.
  3. Feel those bumps. Get comfortable with them.
  4. Don’t make it weird, though.

Congratulations! Now you can do everything without moving your hands!

Vim modes

Mode Key Description
Normal Esc Edit and navigate
Insert i Type text
Visual v Select and edit text
Command : Execute internal commands
External ! Execute external commands

Note

What differentiates vim from other editors is that Normal mode is the default on vim, where other editors use “Insert mode” as the default.

Opening, saving, and closing files

Operation Command
Open (edit) a file :e filename
Save (write to) a file (as) :w (:w filename)
Close (quit) a file (drop changes) :q (:q!)
Close and quit :wq (:x)

Moving around your document

Those f🤬cking arrows…

IBM model M (1986)

ADM-3A (1976)

Tip

j is mapped to “down” because that’s (arguably) the most common direction

Vim words vs. Vim WORDS

According to vim:

words
alphanumeric sequences
WORDS
sequences of text isolated by whitespace

For example:

ThisIsOneWord
these-are.seven((words
this-is.one-WORD

Moving faster

Operation Key
Move to next word w
Move back a word b
Move to the end of a word e
Move to the next WORD W
Move Back a WORD B
Move to the End of a WORD E

Tip

Add a number before the command to repeat it that many times

3w moves 3 words forward, 4k moves 4 lines up

Moving to specific charaters on a line

Operation Command
find next x fx
Find previous x Fx
go until x tx
go unTil x (backwards) Tx
Repeat last operation ;
Undo previous ; ,

Moving to the extreme ends

Operation Command
Start of line 0
First non-whitespace character ^
End of line $
Matching parenthesis/bracket %
Start of file gg
End of file G

Moving vertically

Operation Command
Go up/down a paragraph {, }
Scroll up/down half a screen Ctrl+u, Ctrl+d
Scroll forward/back a full screen Ctrl+f, Ctrl+b
Set cursor to top/zenter/bottom of screen zt, zz, zb
Go to line n nG

Tip

  • :set number shows line numbers
  • :set relativenumber shows relative line numbers
  • :set nonumber norelativenumber hides line numbers

Folding code

Operation Key
open a fold zo
close a fold zc
reveal document (all folds) zr
minimize document (all folds) zm

Tip

:help foldmethod to customize how folds are created

Finding things

Operation Command
Find text /text
Find text backwards ?text
Find next occurrence of searched text n
Find previous occurrence of searched text N
Find next occurence of text under cursor *
Find previous occurence of text under cursor #

Replacing things

Operation Command
Replace text with newtext :s/text/newtext

:s modifiers

  • :s/text/newtext/g to replace all occurrences in the line
  • :s/text/newtext/gc to confirm each replacement
  • :%s/text/newtext/g to replace all occurrences in the file
  • :.,+5s/text/newtext/g to replace all occurrences in the next 5 lines

Editing your document

Selecting things

Operation Command
Visual mode v
Visual line mode V
Visual block mode Ctrl+v

Tip

  • Select word inside parentheses: vi(
  • Select word inside parentheses and parentheses: va(

Adding things

Entering insert mode like a pro:

Operation Command
Insert text before cursor i
Insert text after cursor (i.e., append) a
Add new line below cursor o
Insert at the beginning of line I
Append to end of line A
Add new line above cursor O

Moving fast and deleting things

Operation Command
Replace character with a ra
Enter overwrite mode R
Remove and replace character s
Change text c
Replace whole line S
Replace until end of line C

CutDelete, copyyank, and paste

Operation Command
delete d
yank y, Y
paste p, P

Tip

Always remember the modifiers:

  • delete two words: d2w
  • yank a paragraph: y}

Formatting things

Operation Command
Indent >>
Unindent <<
Join lines J
Toggle case g~
Format selection gq

Tip

Remember the modifiers:

  • Indent the next 3 paragraphs: >3}>
  • Toggle case for the next 3 words: g~3w

ClipboardRegisters

Everything you cut, copy or delete is stored in a register.

Register Description
Default (internal) register
0 The last yanked text
1-9 The last 9 deleted lines
- The last deleted text (shorter than a line)
a-z Exclusive registers just for you

Tip

:reg shows all registers

System clipb… register!

Register Description
+ or * System clipboard (X11, macOS)

Warning

System clipboard is not available in all vim installations. Look for a + on vim --version | grep clipboard.

To use a register, type followed by the register name and the command:

  • +y to yank to system clipboard
  • +p to paste from system clipboard
  • 0p to paste the last yanked text
  • p is the same as p

Macros

Operation Command
Record macro on register “a” qa
Stop recording macro q
Play macro on register “a” @a
Repeat last macro @@

Expanding and configuring vim

" Enable relative number in normal and visual mode
set relativenumber
augroup RelativeNumber
  autocmd!
  autocmd InsertEnter * set norelativenumber
  autocmd InsertLeave * set relativenumber
augroup END

Vim shortcuts everywhere

  • Vimium (browser extension)
  • Vim emulation for VSCode
  • Custom keybindings for your OS (good luck!)
  • Vim-flavored terminal emulator (Alacritty)
  • Vim-flavored file explorer (Ranger)